﻿using System;

namespace _12_ProxyPattern
{
    /// <summary>
    /// 代理模式
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            IImage image = new ProxyImage("pic.jpg");
            //图像将从磁盘加载
            image.Display();
            Console.WriteLine("");
            // 图像不需要从磁盘加载
            image.Display();
            Console.ReadKey();
        }
    }
    public interface IImage
    {
        void Display();
    }
    public class RealImage : IImage
    {
        public string _fileName;
        public RealImage(string fileName)
        {
            _fileName = fileName;
            LoadFromDisk(fileName);
        }
        public void Display()
        {
            Console.WriteLine("显示图片:" + _fileName);
        }
        public void LoadFromDisk(string fileName)
        {
            Console.WriteLine("加载图片:" + fileName);
        }
    }
    public class ProxyImage : IImage
    {
        private RealImage _realImage;
        private string _fileName;
        public ProxyImage(string fileName)
        {
            _fileName = fileName;
        }
        public void Display()
        {
            if (_realImage == null)
            {
                _realImage = new RealImage(_fileName);
            }
            _realImage.Display();
        }
    }
}
